package com.foxzzz.datamemory.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foxzzz.datamemory.entity.BookMetaData;
import com.foxzzz.datamemory.entity.BookNote;
import com.foxzzz.datamemory.service.BookMetaDataService;
import com.foxzzz.datamemory.service.BookNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;


@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookNoteService noteService;
    @Autowired
    private BookMetaDataService metaService;

    public String getFromRequest(HttpServletRequest request){
        StringBuilder sb = new StringBuilder();
        try (ServletInputStream inputStream = request.getInputStream()){
            byte[] b = new byte[4096];
            for (int n; (n = inputStream.read(b)) != -1;)
            {
                sb.append(new String(b, 0, n));
            }
            return sb.toString();
        } catch (Exception e) {
           return null;
        }
    }
    @PostMapping("/pageMeta")
    public Page<BookMetaData> pageMeta(HttpServletRequest request, int page, int size, String order){
        String bookMetaDataStr=getFromRequest(request);
        ObjectMapper objectMapper=new ObjectMapper();
        BookMetaData bookMetaData = null;
        try {
            if(bookMetaDataStr!=null&&!"".equals(bookMetaDataStr)) {
                bookMetaData = objectMapper.readValue(bookMetaDataStr, BookMetaData.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return metaService.page(bookMetaData, page, size, order);
    }

    @PostMapping("/saveMeta")
    public BookMetaData saveMeta(@RequestBody BookMetaData bookMetaData){
        return metaService.save(bookMetaData);
    }

    @DeleteMapping("/deleteMeta")
    public void deleteMeta(String id){
        metaService.delete(id);
    }

    @PostMapping("/pageNote")
    public Page<BookNote> pageNote(HttpServletRequest request, int page, int size, String order){
        String bookNoteStr=getFromRequest(request);
        ObjectMapper objectMapper=new ObjectMapper();
        BookNote bookNote = null;
        try {
            if(bookNoteStr!=null&&!"".equals(bookNoteStr)) {
                bookNote = objectMapper.readValue(bookNoteStr, BookNote.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return noteService.page(bookNote, page, size, order);
    }

    @PostMapping("/listNeedReview")
    public Page<BookNote> listNeedReview(int page, int size){
        return noteService.listNeedReview(page, size);
    }

    @PostMapping("/saveNote")
    public BookNote saveNote(@RequestBody BookNote bookNote){
        return noteService.save(bookNote);
    }


    @DeleteMapping("/deleteNote")
    public void deleteNote(String id){
        noteService.delete(id);
    }

    @PostMapping("/levelUp")
    public BookNote levelUp(String id){
        return noteService.levelUp(id);
    }

    @PostMapping("/reOpen")
    public BookNote reOpen(String id){
        return noteService.reOpen(id);
    }



}
